<?php

declare(strict_types=1);
namespace App\Service;
use App\Constants\ErrorCode;
use App\Dao\WxnoticeDao;
use App\Utils\ServerUtil;
use App\Dao\AdminLogDao; // 日志记录

class WxnoticeService
{
    /** 小程序活动页修改数据
     * @Author abell
     * @DateTime 2020-05-27
     * @return array
     */
    public function editNotice($request){
        try {
            $before_data = make(WxnoticeDao::class)->getNotice($request['id']);
            $update_data['update_time'] = time();
            if(array_key_exists('title',$request)) $update_data['title'] = $request['title'];
            if(array_key_exists('start_time',$request)) $update_data['start_time'] = $request['start_time'];
            if(array_key_exists('end_time',$request)) $update_data['end_time'] = $request['end_time'];
            if(array_key_exists('content',$request)) $update_data['content'] = $request['content'];
            if(array_key_exists('img',$request)) $update_data['img'] = $request['img'];
            if(array_key_exists('type',$request)) $update_data['type'] = $request['type'];
            $result = make(WxnoticeDao::class)->editNotice($request['id'],$update_data);
            make(AdminLogDao::class)->addLog('编辑了微信信息,改前数据“'.json_encode($before_data).'”'.'改后数据“'.json_encode($update_data).'”');
            return ['code'=>ErrorCode::SUCCESS,'msg'=>ErrorCode::getMessage(ErrorCode::SUCCESS),'data'=>$result];
        }catch (\Exception $e) {
            ServerUtil::info('问题反馈类型删除/admin/wxnotice/index', ['error' => $e->getMessage(), 'request' => $request], 'ERROR');
            return ['code'=>ErrorCode::ERR_SERVER,'msg'=>ErrorCode::getMessage(ErrorCode::ERR_SERVER),'data'=>''];
        }
    }

    /** 小程序活动页获取id
     * @Author abell
     * @DateTime 2020-05-27
     * @return array
     */
    public function getNoticeId($request){
        if(array_key_exists('id',$request)){
            $data = make(WxnoticeDao::class)->getNotice($request['id']);
        }else{
            // 没有传id 自动获取最后一个id
            $data = make(WxnoticeDao::class)->lastNotice();
            if(!$data) $new_id = make(WxnoticeDao::class)->initializationNotice();
        }
        return $data ? $data['id'] : $new_id;
    }
    /** 小程序活动页获取 默认获取最后一个
     * @Author abell
     * @DateTime 2020-05-27
     * @return array
     */
    public function getNotice($request){
        try {
            if(array_key_exists('id',$request)){
                $result = make(WxnoticeDao::class)->getNotice($request['id']);
            }else{
                $result = make(WxnoticeDao::class)->lastNotice();
            }
            return ['code'=>ErrorCode::SUCCESS,'msg'=>ErrorCode::getMessage(ErrorCode::SUCCESS),'data'=>$result[0]];
        }catch (\Exception $e){
            ServerUtil::info('小程序活动页获取/api/wxnotice/get_notice',['error'=>$e->getMessage(),'request'=>$request],'ERROR');
            return ['code'=>ErrorCode::ERR_SERVER,'msg'=>ErrorCode::getMessage(ErrorCode::ERR_SERVER),'data'=>''];
        }
    }

    /** 小程序活动页获取列表
     * @Author abell
     * @DateTime 2020-05-27
     * @return array
     */
    public function listNotice(){
        try {
            $result = make(WxnoticeDao::class)->listNotice();
            return ['code'=>ErrorCode::SUCCESS,'msg'=>ErrorCode::getMessage(ErrorCode::SUCCESS),'data'=>$result];
        }catch (\Exception $e){
            ServerUtil::info('小程序活动页列表页获取/api/wxnotice/list_notice',['error'=>$e->getMessage()],'ERROR');
            return ['code'=>ErrorCode::ERR_SERVER,'msg'=>ErrorCode::getMessage(ErrorCode::ERR_SERVER),'data'=>''];
        }
    }
}
